<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { getPiechartOption } from '@/api/statistics'
const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      console.log('init pie chart...')
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      getPiechartOption().then(response => {
        if (response.code === 20000 && response.data) {
          var option = response.data.option
          var legend = response.data.legend
          console.log(option)
          this.chart.setOption({
            title: {
             text: 'Tags Feature',
             subtext: 'TEST',
             left: 'center'
            },
             tooltip: {
                 trigger: 'item',
                 formatter: '{a} <br/>{b} : {c} ({d}%)'
             },
             toolbox: {
               show: true,
               feature: {
                   mark: {show: true},
                   dataView: {show: true, readOnly: false},
                   magicType: {
                       show: true,
                       type: ['pie', 'funnel']
                   },
                   restore: {show: true},
                   saveAsImage: {show: true}
               }
            },
            legend: legend,
            series: option
          })
          console.log(this.chart)
        }
      })
    }
  }
}
</script>
